package vip.aster.common.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import vip.aster.common.constant.enums.CacheTypeEnum;

/**
 * Redisson配置
 *
 * @author Aster
 * @since 2025/4/22 15:09
 */
@Configuration
public class RedissonConfig {
    @Value("${spring.cache.type}")
    private String type;
    @Value("${spring.data.redis.host: localhost}")
    private String host;
    @Value("${spring.data.redis.port: 6379}")
    private String port;
    @Value("${spring.data.redis.password:}")
    private String password;
    @Value("${spring.data.redis.database: 0}")
    private int database;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        if (CacheTypeEnum.REDIS.getValue().equals(type)) {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password).setDatabase(database);
            return Redisson.create(config);
        } else {
            RedissonClient redissonClient = new RedissonClientTemporary();
            return redissonClient;
        }
    }
}
